/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.gui.dummydevice;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * DummyDeviceSettingsDialog
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class DummyDeviceSettingsDialog extends TitleAreaDialog {
	
	private static final int DEFAULT_COORDINATES = 1;
	
	private int x = DEFAULT_COORDINATES;
	private int y = DEFAULT_COORDINATES;
	private int direction;
	
	private Text xText;
	private Text yText;
	private Text directionText;

	public DummyDeviceSettingsDialog(Shell parentShell) {
		super(parentShell);
	}
	
	@Override
	public void create() {
		super.create();
		setTitle("Dummy Device initial configuration");
		setMessage("Please specify default position and direction", IMessageProvider.INFORMATION);
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		parent.setLayout(layout);

		Label xLabel = new Label(parent, SWT.NONE);
		xLabel.setText("X ");

		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		
		xText = new Text(parent, SWT.BORDER);
		xText.setLayoutData(gridData);
		xText.setText(Integer.toString(x));
		
		Label yLabel = new Label(parent, SWT.NONE);
		yLabel.setText("Y ");
		
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		
		yText = new Text(parent, SWT.BORDER);
		yText.setLayoutData(gridData);
		yText.setText(Integer.toString(y));
		
		Label directionLabel = new Label(parent, SWT.NONE);
		directionLabel.setText("Direction ");
		
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		
		directionText = new Text(parent, SWT.BORDER);
		directionText.setLayoutData(gridData);
		directionText.setText(Integer.toString(direction));
		
		return parent;
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 3;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = SWT.CENTER;

		parent.setLayoutData(gridData);
		createOkButton(parent, OK, "Ok", true);
		Button cancelButton = createButton(parent, CANCEL, "Cancel", false);
		
		cancelButton.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				setReturnCode(CANCEL);
				close();
			}
			
		});
	}

	protected Button createOkButton(Composite parent, int id, String label, boolean defaultButton) {
		// increment the number of columns in the button bar
		((GridLayout) parent.getLayout()).numColumns++;
		Button button = new Button(parent, SWT.PUSH);
		button.setText(label);
		button.setFont(JFaceResources.getDialogFont());
		button.setData(new Integer(id));
		button.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent event) {
				if (isValidInput()) {
					okPressed();
				}
			}
			
		});
		
		if (defaultButton) {
			Shell shell = parent.getShell();
			if (shell != null) {
				shell.setDefaultButton(button);
			}
		}
		setButtonLayoutData(button);
		return button;
	}
	
	private boolean isValidInput() {
		boolean valid = true;
		try {
			Integer.parseInt(xText.getText());
		} catch (Exception e) {
			setErrorMessage("Please check the x position. It must be a valid number.");
			valid = false;
		}
		
		try {
			Integer.parseInt(yText.getText());
		} catch (Exception e) {
			setErrorMessage("Please check the y position. It must be a valid number.");
			valid = false;
		}
		
		try {
			int dirValue = Integer.parseInt(directionText.getText());
			if (dirValue > 360 || dirValue < 0) {
				setErrorMessage("Please check the direction. It must be a valid number between 0 and 360 inclusive.");
				valid = false;
			}
		} catch (Exception e) {
			setErrorMessage("Please check the x position. It must be a valid number.");
			valid = false;
		}
		
		return valid;
	}
	
	@Override
	protected boolean isResizable() {
		return true;
	}

	private void saveInput() {
		x = Integer.parseInt(xText.getText());
		y = Integer.parseInt(yText.getText());
		direction = Integer.parseInt(directionText.getText());
	}

	@Override
	protected void okPressed() {
		saveInput();
		super.okPressed();
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public int getDirection() {
		return direction;
	}

	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}
	
}

